package com.agileai.crm.controller.opportunity;

import java.math.BigDecimal;
import java.util.Date;

import com.agileai.crm.bizmoduler.opportunity.OppInfoManage;
import com.agileai.crm.common.PrivilegeHelper;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;

public class OppInfoManageEditHandler
        extends StandardEditHandler {
    public OppInfoManageEditHandler() {
        super();
        this.listHandlerClass = OppInfoManageListHandler.class;
        this.serviceId = buildServiceId(OppInfoManage.class);
    }
    public ViewRenderer prepareDisplay(DataParam param) {
		String operaType = param.get(OperaType.KEY);
		User user = (User) this.getUser();
		if("update".equals(operaType)){
			DataRow record = getService().getRecord(param);
			setAttribute("doEdit8Save", true);
			this.setAttributes(record);	
		}
		if("detail".equals(operaType)){
			DataRow record = getService().getRecord(param);
			PrivilegeHelper privilegehelper = new PrivilegeHelper(user);
			if(privilegehelper.isSalesMan()){
				if("0".equals(record.get("OPP_STATE"))){
					setAttribute("doEdit8Save", true);
					setAttribute("dosubmit", true);
					setAttribute("doConfirm", false);
					setAttribute("doRevokeconfirm", false);
					setAttribute("readOnly", false);
				}
				if("1".equals(record.get("OPP_STATE"))||"2".equals(record.get("OPP_STATE"))){
					setAttribute("doEdit8Save", false);
					setAttribute("dosubmit", false);
					setAttribute("doConfirm", false);
					setAttribute("doRevokeconfirm", false);
					setAttribute("readOnly", true);
				}
				
			}
			if(privilegehelper.isSalesDirector()){
				if("0".equals(record.get("OPP_STATE"))){
					setAttribute("doEdit8Save", true);
					setAttribute("dosubmit", false);
					setAttribute("doConfirm", false);
					setAttribute("doRevokeconfirm", false);
					setAttribute("readOnly", false);
					setAttribute("doAssign", true);
					setAttribute("doClose",true);
				}
				if("1".equals(record.get("OPP_STATE"))){
					setAttribute("doEdit8Save", false);
					setAttribute("dosubmit", false);
					setAttribute("doConfirm", true);
					setAttribute("doRevokeconfirm", false);
					setAttribute("readOnly", false);
					setAttribute("doAssign", true);
					setAttribute("doClose",true);
				}
				if("2".equals(record.get("OPP_STATE"))){
					setAttribute("doEdit8Save", false);
					setAttribute("dosubmit", false);
					setAttribute("doConfirm", false);
					setAttribute("doRevokeconfirm", true);
					setAttribute("readOnly", true);
					setAttribute("doAssign", true);
					setAttribute("doClose",true);
				}
			}
			this.setAttributes(record);	
		}
		if("doDispose".equals(operaType)){
			DataRow record = getService().getRecord(param);
			this.setAttributes(record);	
			setAttribute("doClose",true);
		}
		
		this.setOperaType(operaType);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
    protected void processPageAttributes(DataParam param) {
        setAttribute("OPP_STATE",
                     FormSelectFactory.create("OPP_STATE")
                                      .addSelectedValue(getAttributeValue("OPP_STATE",
                                                                               "0")));
        User user = (User) this.getUser();
		this.setAttribute("OPP_CREATER_NAME",
				this.getAttribute("OPP_CREATER_NAME", user.getUserName()));
		this.setAttribute("OPP_CREATER",
				this.getAttribute("OPP_CREATER", user.getUserId()));
		String ocrtDate = (String) this.getAttribute("OPP_CREATE_TIME",
				DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL,
						new Date()));
		this.setAttribute("OPP_CREATE_TIME", ocrtDate);
        BigDecimal oppMoney = new BigDecimal("0.00");
		if (this.getAttribute("OPP_EXPECT_INVEST") == null) {
			this.setAttribute("OPP_EXPECT_INVEST", oppMoney);
		}
    }
	public ViewRenderer doSaveAction(DataParam param){
		String operateType = param.get(OperaType.KEY);
		if (OperaType.UPDATE.equals(operateType)){
			if(param.get("OPP_EXPECT_INVEST").trim().equals("")){
				param.put("OPP_EXPECT_INVEST","0.00");
			}
			getService().updateRecord(param);	
		}
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
	@PageAction
	public ViewRenderer dosubmit(DataParam param) {
		param.put("OPP_STATE","1");
		getService().changeStateRecord(param);
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
	@PageAction
	public ViewRenderer doconfirm(DataParam param) {
		param.put("OPP_STATE","2");
		getService().changeStateRecord(param);
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
	@PageAction
	public ViewRenderer doRevokeconfirm(DataParam param) {
		param.put("OPP_STATE","0");
		getService().changeStateRecord(param);
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
	@PageAction
	public ViewRenderer doClose(DataParam param) {
		param.put("OPP_STATE", "3");
		getService().changeStateRecord(param);
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
	@PageAction
	public ViewRenderer doCreateOppContAction(DataParam param) {
		storeParam(param);
		String a = "OppCreateContEdit";
		return new DispatchRenderer(
				getHandlerURL(a)
						+ "&"
						+ OperaType.KEY
						+ "=doCreateOppContAction&comeFrome=doCreateOppContAction");
	}
    protected OppInfoManage getService() {
        return (OppInfoManage) this.lookupService(this.getServiceId());
    }
}
